package com.ro.model.blog;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.redis.Redis;
import com.ro.common.model.Blog;

import java.util.Date;
import java.util.List;

public class BlogService {

    private static final Blog dao = new Blog().dao();

    public Page<Blog> paginate(int pageNumber, int pageSize, String seatchStr) {
        StringBuffer sql = new StringBuffer();
        if (seatchStr != null && !"".equals(seatchStr)) {
            sql.append(" and (").append(" title like '%").append(seatchStr).append("%'")
                    .append(" or key_word like '%").append(seatchStr).append("%'")
                    .append(")");
        }
        Page<Blog> blogPage = dao.paginate(pageNumber, pageSize, "select `id`,`title`,`describe`,`content`,`read_num`,`key_word`,`state`,`sort`,`create_time`,`del_flag`",
                "from blog where del_flag = 1 AND state = 1 " + sql + " order by sort desc");
        return blogPage;
    }

    public Page<Blog> paginateManage(int pageNumber, int pageSize, String seatchStr) {
        StringBuffer sql = new StringBuffer();
        if (seatchStr != null && !"".equals(seatchStr)) {
            sql.append(" and (").append(" title like '%").append(seatchStr).append("%'")
                    .append(" or key_word like '%").append(seatchStr).append("%'")
                    .append(")");
        }
        Page<Blog> blogPage = dao.paginate(pageNumber, pageSize, "select `id`,`title`,`describe`,`content`,`read_num`,`key_word`,`state`,`sort`,`create_time`,`del_flag`",
                "from blog where del_flag = 1 " + sql + " order by sort desc");
        return blogPage;
    }

    public Blog findById(int id) {
        return dao.findById(id);
    }

    //删除文章
    public void deleteById(int id) {
        Blog blog = new Blog();
        blog.setId(id).setDelFlag(2).update();
        Redis.use("ec").set(id, null);
    }

    //缓存中读取数据,没有就从数据库中读取后,放到缓存
    public Blog getBlogEc(int id) {
        Blog blog = null;
        blog = Redis.use("ec").get(id);
        if (blog == null) {
            blog = findById(id);
        }
        return blog;
    }


    //修改文章
    public void updateBlog(Blog blog) {
        blog.update();
        Redis.use("ec").set(blog.getId(), blog);
    }

    //创建文章
    public void createBlog(Blog blog) {
        blog.setCreateTime(new Date()).setSort(geMaxSortNum()).save();
        Redis.use("ec").set(blog.getId(), blog);
    }

    //刷新缓存
    public void refreshCache() {
        List<Blog> blogs = dao.find("select `id`,`title`,`describe`,`content`,`read_num`,`key_word`,`state`,`sort`,`create_time`,`del_flag` from blog where del_flag = 1");
        blogs.forEach(item -> {
            Redis.use("ec").set(item.getId(), item);
        });
    }

    //获取最大排序值
    private int geMaxSortNum() {
        Integer maxSortNum = dao.findFirst("select MAX(`sort`) as `sort` from blog").getSort();
        if (maxSortNum == null) {
            maxSortNum = 1;
        } else {
            maxSortNum++;
        }
        return maxSortNum;
    }

    //修改文章阅读次数
    public void updateAddReadNum(int id) {
        Db.update("update blog set read_num = read_num + 1 where id = ? and del_flag = 1",id);
    }
}
